package com.gexiaobao.pigeon.ui.fragment.match

import android.os.Bundle
import androidx.core.content.ContextCompat
import com.bin.david.form.core.SmartTable
import com.bin.david.form.data.style.FontStyle
import com.bin.david.form.data.style.LineStyle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.GroupSignResponse
import com.gexiaobao.pigeon.databinding.FragmentLookSignUpBinding
import com.gexiaobao.pigeon.ui.race.RaceViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 10:51
 * @Describe    : 插组-查看报名
 */
class FragmentLookSignUp : BaseFragment<RaceViewModel, FragmentLookSignUpBinding>() {

    private var id: String = ""
    lateinit var data: SmartTable<GroupSignResponse.DataInfo>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includedTitleBarMenu.toolbar.initClose("报名") {
            nav().navigateUp()
        }
        mDatabind.includedTitleBarMenu.tvRightMenu.text = "刷新"
        id = requireArguments().getInt("id").toString()
        initSmartTable()
//        mDatabind.swipeRefreshLayout.init {
//            showLoading(getString(R.string.loading))
//            mViewModel.getSignList(id, "", "")
//        }
    }

    private fun initSmartTable() {
        data = mDatabind.smartTable as SmartTable<GroupSignResponse.DataInfo>
        val fontStyle = context?.let { ContextCompat.getColor(it, R.color.black) }
            ?.let { FontStyle(context, 18, it) }
        val contentStyle = context?.let { ContextCompat.getColor(it, R.color.comm_text_gray) }
            ?.let { FontStyle(context, 16, it) }
        val lineStyle = LineStyle()
        lineStyle.color = context?.let { ContextCompat.getColor(it, R.color.dedede) }!!
        data.config.columnTitleStyle = fontStyle
        data.config.contentStyle = contentStyle
        data.config.setHorizontalPadding(20)
            .setVerticalPadding(40)
            .setSequenceHorizontalPadding(20)
            .setSequenceVerticalPadding(40)
            .setShowTableTitle(false) // 去除标题
            .setFixedFirstColumn(true) //第一行作废
            .setShowXSequence(false)
            .setShowYSequence(false)
            .isFixedYSequence = true
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.getSignList(id, "", "")
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.groupSignResult.observe(viewLifecycleOwner) {
//            if (mDatabind.swipeRefreshLayout.isRefreshing) {
//                mDatabind.swipeRefreshLayout.isRefreshing = false
//            }
            dismissLoading()
            setSmartTableData(it)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includedTitleBarMenu.tvRightMenu) {
            when (it) {
                mDatabind.includedTitleBarMenu.tvRightMenu -> {
                    showLoading(getString(R.string.loading))
                    mViewModel.getSignList(id, "", "")
                }
            }
        }
    }

    private fun setSmartTableData(it: GroupSignResponse?) {
        val listData = ArrayList<GroupSignResponse.DataInfo>()
        if (it != null) {
            if (it.data.isNotEmpty()) {
                it.data.forEachIndexed { index, dataInfo ->
                    listData.add(index, dataInfo)
                    dataInfo.orderNumber = index + 1
                    val ringId = dataInfo.ringId
                    if (ringId.contains(",")) {
                        val str = ringId.split(",")
                        if (str.isNotEmpty()) {
                            val sb = StringBuilder()
                            for (i in str.indices) {
                                val result = sb.append(str[i] + "\n")
                                dataInfo.ringId = result.toString()
                            }
                        }
                    }
                }
                data.setData(listData)
            }
        }
    }
}